package com.banmao.mall.service.client.impl;

import com.banmao.mall.mapper.CommentMapper;
import com.banmao.mall.mapper.UserMapper;
import com.banmao.mall.model.bo.client.comment.CommentListBO;
import com.banmao.mall.model.pojo.Comment;
import com.banmao.mall.model.pojo.User;
import com.banmao.mall.model.vo.client.comment.CommentCountVO;
import com.banmao.mall.model.vo.client.comment.CommentListWxVO;
import com.banmao.mall.model.vo.client.comment.CommentOfCommentVO;
import com.banmao.mall.model.vo.client.comment.UserOfCommentVO;
import com.banmao.mall.service.client.CommentServiceWx;
import com.banmao.mall.util.CustomUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceWxImpl implements CommentServiceWx {

    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;

    @Value("${myimage.baseURL}")
    private String imageURL;
    @Override
    public CommentListWxVO select(CommentListBO commentListBO) {
        /**
         * valueId：商品/专题/订单id
         * showType：全部评论（0）/有图（1）
         */
        //开启分页
        PageHelper.startPage(commentListBO.getPage(),commentListBO.getSize());

        CommentListWxVO commentListWxVO = new CommentListWxVO();
        //根据BO从数据库获取相应的评论数据
        List<Comment> commentList = commentMapper.selectByRequire(commentListBO);
        //将带图片的评论的图片地址添加前缀
        for (Comment comment : commentList) {
            if (comment.getHasPicture()) {
                String[] picUrlList = comment.getPicUrls();
                for (int i = 0; i < picUrlList.length; i++) {
                    if (!picUrlList[i].startsWith("http")) {
                        picUrlList[i]=imageURL+picUrlList[i];
                    }
                }
                comment.setPicUrls(picUrlList);
            }
        }

        PageInfo<Comment> pageInfo = new PageInfo<>(commentList);
        long total = pageInfo.getTotal();

        List<CommentOfCommentVO> data = new ArrayList<>();
        //因为CommentListWxVO中的data是List<CommentOfCommentVO>类型，将数据库查询到的commentList中的
        //数据转入进data
        for (Comment comment : commentList) {
            CommentOfCommentVO commentOfCommentVO = new CommentOfCommentVO();
            commentOfCommentVO.setAddTime(comment.getAddTime());
            commentOfCommentVO.setContent(comment.getContent());
            commentOfCommentVO.setPicList(comment.getPicUrls());

            //将commentList中的数据转移后，data还需获取发该评论的用户的信息
            User user = userMapper.selectByPrimaryKey(comment.getUserId());
            //用户头像图片添加前缀
            if (!user.getAvatar().startsWith("http")) {
                user.setAvatar(imageURL+user.getAvatar());
            }
            UserOfCommentVO userInfo = new UserOfCommentVO();
            userInfo.setNickname(user.getNickname());
            userInfo.setAvatarUrl(user.getAvatar());
            commentOfCommentVO.setUserInfo(userInfo);

            data.add(commentOfCommentVO);
        }
        commentListWxVO.setData(data);
        commentListWxVO.setCount((int) total);
        commentListWxVO.setCurrentPage(commentListBO.getPage());
        return commentListWxVO;
    }

    @Override
    public CommentCountVO count(Integer valueId, Byte type) {
        CommentCountVO commentCountVO = new CommentCountVO();
        int allCount = commentMapper.selectCountByRequire(valueId,type, (byte) 0);
        int hasPicCount = commentMapper.selectCountByRequire(valueId,type, (byte) 1);
        commentCountVO.setAllCount(allCount);
        commentCountVO.setHasPicCount(hasPicCount);
        return commentCountVO;
    }

    @Override
    public Comment post(Comment commentBO) throws IllegalAccessException {
        Date date = new Date();
        commentBO.setAddTime(date);
        commentBO.setUpdateTime(date);
        Integer userId = CustomUtils.judgeUserLogin();
        commentBO.setUserId(userId);
        commentBO.setDeleted(false);
        int insert = commentMapper.insert(commentBO);
        return commentBO;
    }
}
